<?php

/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Sitepageinvite
 * @copyright  Copyright 2010-2011 BigStep Technologies Pvt. Ltd.
 * @license    http://www.socialengineaddons.com/license/
 * @version    $Id: install.php 2011-05-05 9:40:21Z SocialEngineAddOns $
 * @author     SocialEngineAddOns
 */
class Sitepageinvite_Installer extends Engine_Package_Installer_Module {
	
	function onPreInstall() {
		$db = $this->getDb ();
		
		//CHECK THAT SITEPAGE PLUGIN IS ACTIVATED OR NOT
		$select = new Zend_Db_Select ( $db );
		$select->from ( 'engine4_core_settings' )->where ( 'name = ?', 'sitepage.is.active' )->limit ( 1 );
		$sitepage_settings = $select->query ()->fetchAll ();
		if (! empty ( $sitepage_settings )) {
			$sitepage_is_active = $sitepage_settings [0] ['value'];
		} else {
			$sitepage_is_active = 0;
		}
		
		//CHECK THAT SITEPAGE PLUGIN IS INSTALLED OR NOT
		$select = new Zend_Db_Select ( $db );
		$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'sitepage' )->where ( 'enabled = ?', 1 );
		$check_sitepage = $select->query ()->fetchObject ();
		if (! empty ( $check_sitepage ) && ! empty ( $sitepage_is_active )) {
			$PRODUCT_TYPE = 'sitepageinvite';
			$PLUGIN_TITLE = 'Sitepageinvite';
			$PLUGIN_VERSION = '4.1.8';
			$PLUGIN_CATEGORY = 'plugin';
			$PRODUCT_DESCRIPTION = 'Sitepageinvite Plugin';
			$PRODUCT_TITLE = 'Directory / Pages - Inviter Extension';
			$_PRODUCT_FINAL_FILE = 0;
			$SocialEngineAddOns_version = '4.1.8';
			$file_path = APPLICATION_PATH . "/application/modules/$PLUGIN_TITLE/controllers/license/ilicense.php";
			$is_file = file_exists ( $file_path );
			if (empty ( $is_file )) {
				include APPLICATION_PATH . "/application/modules/Sitepage/controllers/license/license4.php";
			} else {
				include $file_path;
			}
			
			$db->query ( 'INSERT IGNORE INTO `engine4_core_settings` ( `name` , `value` ) VALUES ( "pageinvite.show.webmail",  \'a:4:{i:0;s:5:"gmail";i:1;s:5:"yahoo";i:2;s:11:"window_mail";i:3;s:3:"aol";}\')' );
			
			$pageTime = time ();
			$db->query ( "INSERT IGNORE INTO `engine4_core_settings` (`name`, `value`) VALUES
			('sitepageinvite.basetime', $pageTime ),
			('sitepageinvite.isvar', 0 ),
			('sitepageinvite.filepath', 'Sitepageinvite/controllers/license/license2.php');" );
			
			parent::onPreInstall ();
		} elseif (! empty ( $check_sitepage ) && empty ( $sitepage_is_active )) {
			$baseUrl = $_SERVER ['HTTP_HOST'] . Zend_Controller_Front::getInstance ()->getBaseUrl ();
			$url_string = Zend_Controller_Front::getInstance ()->getRequest ()->getRequestUri ();
			if (strstr ( $url_string, "manage/install" )) {
				$calling_from = 'install';
			} else if (strstr ( $url_string, "manage/query" )) {
				$calling_from = 'queary';
			}
			$explode_base_url = explode ( "/", $baseUrl );
			foreach ( $explode_base_url as $url_key ) {
				if ($url_key != 'install') {
					$core_final_url .= $url_key . '/';
				}
			}
			
			return $this->_error ( "<span style='color:red'>Note: You have installed the Page Plugin but not activated it on your site yet. Please activate it first before installing the Page Invite Plugin.</span><br/> <a href='" . 'http://' . $core_final_url . "admin/sitepage/settings/readme'>Click here</a> to activate the Page Plugin." );
		} else {
			$base_url = Zend_Controller_Front::getInstance ()->getBaseUrl ();
			return $this->_error ( "<span style='color:red'>Note: You have not installed the Page Plugin on your site yet. Please install it first before installing the Page Invite Plugin.</span><br/> <a href='" . $base_url . "/manage'>Click here</a> to go Manage Packages." );
		}
	}

}

?>